﻿/***********************************************************************
 * <copyright file="FrmXtraBaseCategoryDetail.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Monday, February 10, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using WindowsForms.AOnline.WindowsForm.BaseUserControls;
using WindowsForms.AOnline.WindowsForm.CommonClass;
using WindowsForms.AOnline.WindowsForm.Resources;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Mask;


namespace WindowsForms.AOnline.WindowsForm.FormBase
{
    public partial class FrmXtraBaseCategoryDetail : XtraForm
    {
        #region Variables

        /// <summary>
        /// Action Mode
        /// </summary>
        public enum ActionModeEnum
        {
            AddNew = 0,
            Edit = 1,
            Delete = 2,
            None = 3
        }
        /// <summary>
        /// Gets or sets the e action mode.
        /// </summary>
        /// <value>
        /// The e action mode.
        /// </value>
        public ActionModeEnum ActionMode;

        /// <summary>
        /// The key for send
        /// </summary>
        private string _keyForSend;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the form caption.
        /// </summary>
        /// <value>
        /// The form caption.
        /// </value>
        [Category("BaseProperty")]
        public string FormCaption { get; set; }

        /// <summary>
        /// Gets or sets the key value.
        /// </summary>
        /// <value>
        /// The key value.
        /// </value>
        public string KeyValue { get; set; }

        /// <summary>
        /// Gets or sets the binding source detail.
        /// </summary>
        /// <value>
        /// The binding source detail.
        /// </value>
        public BindingSource BindingSourceDetail { get; set; }

        #endregion

        #region Functions

        /// <summary>
        /// Initializes the layout.
        /// </summary>
        private void InitializeLayout()
        {
            Text = ActionMode == ActionModeEnum.AddNew ? string.Format(ResourceHelper.GetResourceValueByName("ResAddNewText"), FormCaption) : string.Format(ResourceHelper.GetResourceValueByName("ResEditText"), FormCaption);
            InitControls();
        }

        /// <summary>
        /// Closes the form.
        /// </summary>
        private void CloseForm()
        {
            using (new BaseListUserControl())
            {
                if (PostKeyValue != null) PostKeyValue(this, _keyForSend);
                Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="keyValue">The key value.</param>
        public delegate void EventPostKeyHandler(object sender, string keyValue);

        /// <summary>
        /// The key value
        /// </summary>
        public event EventPostKeyHandler PostKeyValue;

        #endregion

        #region Functions overrides

        /// <summary>
        /// Shows the help.
        /// </summary>
        protected virtual void ShowHelp()
        {
        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidData()
        {
            return true;
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected virtual string SaveData()
        {
            return "";
        }

        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected virtual void InitData()
        {
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected virtual void InitControls()
        {
        }

        /// <summary>
        /// Bindings the data to control.
        /// </summary>
        /// <param name="oContainer">The o container.</param>
        public virtual void BindingDataToControl(Control oContainer)
        {
            try
            {
                if (BindingSourceDetail == null) return;
                foreach (Control oControl in oContainer.Controls)
                {
                    var strTag = (string)oControl.Tag;
                    if (!string.IsNullOrEmpty(strTag))
                    {
                        if (ReferenceEquals(oControl.GetType(), typeof(TextEdit)) || ReferenceEquals(oControl.GetType(), typeof(Label)))
                        {
                            oControl.DataBindings.Add("Text", BindingSourceDetail, strTag);
                        }
                        else
                        {
                            if (oControl.GetType().GetProperty("Checked") != null)
                            {
                                oControl.DataBindings.Add("Checked", BindingSourceDetail, strTag);
                            }
                            else
                            {
                                oControl.DataBindings.Add(
                                    oControl.GetType() == typeof(ComboBoxEdit) ? "SelectedIndex" : "EditValue",
                                    BindingSourceDetail, strTag);
                            }
                        }
                    }
                    else
                    {
                        if (oControl.Controls.Count > 0)
                        {
                            BindingDataToControl(oControl);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(),
                    ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Formats the control.
        /// </summary>
        protected void FormatControl(Control oContainer)
        {
            foreach (Control control in oContainer.Controls)
            {
                if (control.GetType() == typeof(DateEdit))
                {

                    ((DateEdit)control).Properties.Mask.Culture = Thread.CurrentThread.CurrentCulture;
                    ((DateEdit)control).Properties.Mask.UseMaskAsDisplayFormat = true;
                }
                if (control.GetType() == typeof(SpinEdit))
                {
                    if ((string)control.Tag == ControlValueType.Quantity.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "n0";
                    }
                    if ((string)control.Tag == ControlValueType.Year.GetDescription())
                    {
                        ((SpinEdit)control).Properties.Mask.MaskType = MaskType.RegEx;
                        ((SpinEdit)control).Properties.EditMask = @"\d{0,4}";
                    }
                    if ((string)control.Tag == ControlValueType.ExchangeRate.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "c4";
                    }
                    if ((string)control.Tag == ControlValueType.Percent.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "P2";
                    }
                    if ((string)control.Tag == ControlValueType.Money.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "c2";
                    }
                    ((SpinEdit)control).Properties.Mask.Culture = Thread.CurrentThread.CurrentCulture;
                    ((SpinEdit)control).Properties.Mask.UseMaskAsDisplayFormat = true;
                }
                if (control.GetType() == typeof(CalcEdit))
                {
                    if ((string)control.Tag == ControlValueType.Quantity.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "n0";
                    }
                    if ((string)control.Tag == ControlValueType.Year.GetDescription())
                    {
                        ((CalcEdit)control).Properties.Mask.MaskType = MaskType.RegEx;
                        ((CalcEdit)control).Properties.EditMask = @"\d{0,4}";
                    }
                    if ((string)control.Tag == ControlValueType.ExchangeRate.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "c4";
                    }
                    if ((string)control.Tag == ControlValueType.Percent.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "P2";
                    }
                    if ((string)control.Tag == ControlValueType.Money.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "c2";
                    }
                    ((CalcEdit)control).Properties.Mask.Culture = Thread.CurrentThread.CurrentCulture;
                    ((CalcEdit)control).Properties.Mask.UseMaskAsDisplayFormat = true;
                }
                if (control.Controls.Count > 0)
                {
                    FormatControl(control);
                }
            }
        }

        #endregion

        public FrmXtraBaseCategoryDetail()
        {
            InitializeComponent();
        }

        private void FrmXtraBaseCategoryDetail_Load(object sender, EventArgs e)
        {
            try
            {
                if (DesignMode) return;
                InitializeLayout();
                InitData();
                FormatControl(this);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            ShowHelp();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidData())
                {
                        var rowAffected = SaveData();
                        if (rowAffected == null)
                        {
                            if (ActionMode != ActionModeEnum.AddNew)
                            {
                                _keyForSend = KeyValue;
                            }
                            else
                            {

                            }
                         //   _keyForSend = ActionMode == ActionModeEnum.AddNew ? rowAffected.ToString(CultureInfo.InvariantCulture) : KeyValue;
                            CloseForm();
                        }
                        //if (rowAffected > 0)
                        //{
                        //    _keyForSend = ActionMode == ActionModeEnum.AddNew ? rowAffected.ToString(CultureInfo.InvariantCulture) : KeyValue;
                        //    CloseForm();
                        //}
                        else
                        {
                            XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResSaveDataError"), ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Information);

                            XtraMessageBox.Show(rowAffected, ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                }
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}